require("States")
require("BattleManager")

AI = {
    seekAndDestroy_CommonGun = {
        [States.IDLE] = function(entity)
            local enemy = entity:firstEnemyNextToMe()
            if (enemy) then
                entity.aux.prevState = States.IDLE
                entity:attack(enemy)
            end
        end,
        [States.WALK] = function(entity)
            entity:move()
            local enemy = entity:firstEnemyNextToMe()
            if (enemy) then
                entity.aux.prevState = States.WALK
                entity:attack(enemy)
            end
        end,
        [States.ATTACK] = function(entity)
            local enemy = entity.enemy            

            if enemy and entity:reach(enemy) and enemy.state ~= States.DEAD then
                BattleManager.processCombatWithCommonGun(entity, enemy)

            elseif enemy.state == States.DEAD then
                entity:setState(entity.aux.prevState)

            else
                entity:moveTo(enemy.position.x, enemy.position.y)
            end
        end,
        [States.DEAD] = function(entity) end,
        [States.RELOADING] = function(entity) end
    },

	seekAndDestroy_BulletGun = {
        [States.IDLE] = function(entity)
            local enemy = entity:firstEnemyNextToMe()
            if (enemy) then
                entity.aux.prevState = States.IDLE
                entity:attack(enemy)
            end
        end,
        [States.WALK] = function(entity)
            entity:move()
            local enemy = entity:firstEnemyNextToMe()
            if (enemy) then
                entity.aux.prevState = States.WALK
                entity:attack(enemy)
            end
        end,
        [States.ATTACK] = function(entity)
            local enemy = entity.enemy
			
			if entity.aux.bullet and entity.aux.bullet:isActive() then
				entity.sprite:setState(States.IDLE);
            elseif enemy and entity:reach(enemy) and enemy.state ~= States.DEAD then
				entity.sprite:setState(States.ATTACK);	
				if entity.sprite.frame == #entity.origin.sprites.ATTACK then
	                local ePos = {x=enemy.center.x,y=enemy.center.y} 
					local bullet = entity.world:createBullet(entity,ePos);
					entity.aux.bullet = bullet;
				end

            elseif enemy.state == States.DEAD then
                entity:setState(entity.aux.prevState)

            else
                entity:moveTo(enemy.position.x, enemy.position.y)
            end
        end,
        [States.DEAD] = function(entity) end,
        [States.RELOADING] = function(entity) end
    },
	

    base = {
        [States.IDLE] = function(entity) end,
        [States.WALK] = function(entity) end,
        [States.ATTACK] = function(entity) end,
        [States.DEAD] = function(entity) end,
        [States.RELOADING] = function(entity) end
    }
}
